package zqm.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;
import zqm.common.Result;
import zqm.entity.Collect;
import zqm.service.ICollectService;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/collect")
public class CollectController {
    @Resource
    private ICollectService collectService;

    // 新增收藏
    @PostMapping
    public Result add(@RequestBody Collect collect) {
        collectService.save(collect);
        return Result.success();
    }

    // 分页查询收藏
    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize,
                           @RequestParam(required = false) Integer userId) {
        QueryWrapper<Collect> queryWrapper = new QueryWrapper<>();
        if (userId != null) {
            queryWrapper.eq("user_id", userId);
        }
        queryWrapper.orderByDesc("id");
        return Result.success(collectService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }

    // 删除收藏
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        collectService.removeById(id);
        return Result.success();
    }

    // 批量删除
    @PostMapping("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        collectService.removeByIds(ids);
        return Result.success();
    }
} 